
package ProjectTeam39.Domains;
/**
 * AppointmentCalender.java
 * This class represents a "calendar" of days which hold their respective appointments.
 * 
 * @author SeanPohjalainen 
 * @version 1.0
 */
public class AppointmentCalender {
	/** the days of the week */
	public enum Day {
		MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
	}
	/** the data structure that represents the calendar */
	private DoctorDay[][] calendar;
	/** an array of all current doctor names */
	private String[] doctors;
	/** the current number of doctors */
	private int numDocs; 
	/** the length of the month in days */
	private int monthLength;
	
	/**
	 * Creates a new AppointmentCalender object.
	 * 
	 * @param days the number of days in the month created
	 * @param firstday the first day of the month (ie monday, tuesday, ...)
	 * @param docs the string of doctor's names
	 */
	public AppointmentCalender(int days, Date.Day firstday, String[] docs){
		numDocs = docs.length; //length of array holding current doctor names
		doctors = docs;
		monthLength = days;
		calendar = new DoctorDay[numDocs][monthLength];
		int d=0;
		switch(firstday){
			case MONDAY: d = 1; break;
			case TUESDAY: d = 2; break;
			case WEDNESDAY: d = 3; break;
			case THURSDAY: d = 4; break;
			case FRIDAY: d = 5; break;
			case SATURDAY: d = 6; break;
			case SUNDAY: d = 7; break;
		}
		System.out.println(d);
		// for each day of the month, create a schedule for each doctor.
		for (int i = 0; i < monthLength; i++){
			for (int j = 0; j < numDocs; j++){
					calendar[j][i] = new DoctorDay(doctors[j]);
			}
		}
		if (d==6){
			int i = 0;
			while (i<monthLength){
				for (int j = 0; j<numDocs; j++){
					calendar[j][i]=null;
					if (calendar[j][i]==null) System.out.println("this day was nulled during creation");
					if ((i+1)<monthLength){
						calendar[j][i+1]=null;
						if (calendar[j][i+1]==null) System.out.println("this day was nulled during creation");
					}
				}
				i+=7;
			}
		}
		if (d==7){
			int i = 0;
			while (i<monthLength){
				for (int j = 0; j<numDocs; j++){
					calendar[j][i]=null;
					if (calendar[j][i]==null) System.out.println("this day was nulled during creation");
					if ((i-1)>0){
						calendar[j][i+1]=null;
						if (calendar[j][i+1]==null) System.out.println("this day was nulled during creation");
					}
				}
				i+=7;
			}
		}
	}
	/**
	 * If the requested appt time is available, the method creates an Appointment and adds it to
	 * the AppointmentCalender. If it's not, the appointment isn't scheduled.
	 * 
	 * @param p
	 * @param t time
	 * @param d
	 * @param r
	 * @param doc
	 * @return an Appointment if successful, null if unsuccessful
	 */
	public Appointment scheduleAppt(Patient p, int t, Date d, String r, Doctor doc){
		int docNum = getDoctorNum(doc.getName());
		if (this.searchAvailAppt(t, d, doc)){
			System.out.println("scheduleAppt returns an appt");
			Appointment appt = new Appointment(p, t, d, r, doc);
			DoctorDay dd = calendar[docNum][d.getDay()-1];
			appt = dd.addAppt(appt);
			return appt;
		}
		else { //this.searchAvailAppt(t, d, doc) returns false, meaning the requested slot is occupied
			System.out.println("Schedule appt returns null, issue must be with reference not being null?");
			return null;
		}
	}
	/**
	 * Checks whether the requested appointment slot is available.
	 * 
	 * @param time
	 * @param d
	 * @param doc
	 * @return true if it's available, false otherwise
	 */
	public boolean searchAvailAppt(int time, Date d, Doctor doc){
		int docNum = getDoctorNum(doc.getName());
		DoctorDay dd = calendar[docNum][d.getDay()-1];
		// add in if statement to catch sat and sun appts

		if((dd!=null)&&(dd.searchDDAppt(time)==null)){
			System.out.println("searchAvailAppt returns true");
			return true;
		}
		else{
			System.out.println("searchAvailAppt returns false");
			return false;
		}
	}
	/**
	 * Converts a doctors name into their corresponding index in the appointment calendar.
	 * 
	 * @param name
	 * @return an int representing their corresponding array index
	 */
	private int getDoctorNum(String name){
		int q = -1;
		for (int i = 0; i<numDocs; i++){
			if ((doctors[i]).equalsIgnoreCase(name)){
				q = i;
			}
		}
		return q;
	}
	/**
	 * Searches for, and returns, the contents of the data structure location corresponding 
	 * to the given params.
	 * 
	 * @param time
	 * @param d
	 * @param doc
	 * @return an Appointment object or null
	 */
	public Appointment searchAppt(int time, Date d, Doctor doc){
		int docNum = getDoctorNum(doc.getName());
		DoctorDay dd = calendar[docNum][d.getDay()-1];
		if (dd==null){
			return null;
		}
		return dd.searchDDAppt(time);
	}
	public void print(){
		for(int i = 0; i<monthLength; i++){
			if (calendar[1][i]!=null) System.out.println("this day isn't null(=>weekday)");
			else System.out.println("this day is null");
		}
	}
}
